package org.example;

class MyLinkedList {
    int size;
    ListNode head;
    public MyLinkedList() {
        this.size = 0;
        head = new ListNode();
    }

    public int get(int index) {
        if (index < 0 || index >= this.size) return -1;
        ListNode cur = head.next;
        while (index > 0) {
            cur = cur.next;
            index--;
        }
        return cur.val;
    }

    public void addAtHead(int val) {
        ListNode node = new ListNode(val);
        node.next = head.next;
        head.next = node;
        this.size++;
    }

    public void addAtTail(int val) {
        ListNode node = new ListNode(val);
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
        this.size++;
    }

    public void addAtIndex(int index, int val) {
        if (index < 0 || index > this.size) return;
        ListNode cur = head;
        ListNode node = new ListNode(val);
        while (index > 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur.next;
        cur.next = node;
        this.size++;
    }

    public void deleteAtIndex(int index) {
        if (index < 0 || index >= this.size) return;
        ListNode cur = head;
        while (index > 0) {
            cur = cur.next;
            index--;
        }
        cur.next = cur.next.next;
        this.size--;
    }
}

public class Test4 {

}
